// pages/user/userWithdraw/index.js
import urls from '../../../requestUrl'
import request from '../../../baseRequest'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        allowIncome: 0,
        withdrawCout: 0,
        allowCompanyIncome: 0
    },


    /**用户当前可提现额度 */
    getUserIncomeAllow() {
        let url = `${urls.userWallet.getIncomAllow}/${this.data.openid}`
        request.getRequest(url, {}, (res) => {
            console.log('getIncomAllow', res)
            this.setData({
                allowIncome: res.allowIncome
            })
        }, () => {})
    },

    /**公司当前可提现额度 */
    getCompanyIncomeAllow() {
        let url = `${urls.userWallet.getCompanyIncomAllow}/${this.data.openid}`
        request.getRequest(url, {}, (res) => {
            console.log('getCompanyIncomAllow', res)
            this.setData({
                allowCompanyIncome: res.allowIncome
            })
        }, () => {})
    },

    /**监听输入*/
    changInputValue(e) {
        console.log(e.detail.value)
        this.data.withdrawCout = Number(e.detail.value)
    },

    /**用户提现 */
    handelWithDrawIncome(e) {
        if(this.data.withdrawCout == 0){
            wx.showToast({
                title: '请输入提现金额',
                icon: 'none'
            })
            return; 
        }
        if(this.data.withdrawCout < 1){
            wx.showToast({
                title: '提现金额要大于1元',
                icon: 'none'
            })
            return; 
        }
        if (this.data.allowIncome < this.data.withdrawCout) {
            wx.showToast({
                title: '提现金额大于可提现金额',
                icon: 'none'
            })
            return;
        }
        let param = {
            "openid": this.data.openid,
            "withdrawCout": this.data.withdrawCout
        }

        request.postRequest(urls.userWallet.withdrawIncome, param, (res) => {
            console.log(res);
            if(!res.hasError){
               wx.showModal({
                title: '温馨提示',
                content: '恭喜您提现成功!!',
                showCancel: false,
                success: res => {
                    wx.redirectTo({
                      url: '/pages/user/userWallet/index',
                    })
                }
            }) 
            }else{
                wx.showModal({
                    title: '温馨提示',
                    content: '提现失败：'+res.message,
                    showCancel: false,
                    success: res => {
                        wx.redirectTo({
                          url: '/pages/user/userWallet/index',
                        })
                    }
                }) 
            }

        }, () => {
            // wx.showModal({
            //     title: '温馨提示',
            //     content: '恭喜您提现成功!!',
            //     showCancel: false,
            //     success: res => {
            //         wx.redirectTo({
            //           url: '/pages/user/userWallet/index',
            //         })
            //     }
            // })
        })
    },


    /**公司提现 */
    handelWithDrawCompanyIncome(e) {
        if (this.data.withdrawCout == 0 || this.data.allowCompanyIncome < this.data.withdrawCout) {
            wx.showToast({
                title: '提现金额有误，请重试',
                icon: 'none'
            })
            return;
        }
        let param = {
            "openid": this.data.openid,
            "withdrawCout": this.data.withdrawCout
        }

        request.postRequest(urls.userWallet.withdrawCompanyIncome, param, (res) => {}, () => {
            wx.showModal({
                title: '温馨提示',
                content: '恭喜您提现成功!!',
                showCancel: false,
                success: res => {
                    this.getCompanyIncomeAllow()
                }
            })
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.openid = getApp().getUserOpenid()
        let _type = options.type
        this.setData({
            type: _type
        })
        if (_type === '1') {
            this.getUserIncomeAllow()
        }
        if (_type === '2') {
            this.getCompanyIncomeAllow()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})